package com.tianji.learning.mq;


import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.utils.CollUtils;
import com.tianji.learning.service.ILearningLessonService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;


@Slf4j
@Component
@RequiredArgsConstructor
public class LessonChangeListener {


    private final ILearningLessonService lessonService;

    // mq监听订单支付成功
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "learning.lesson.pay.queue", durable = "true"),
            exchange = @Exchange(name = MqConstants.Exchange.ORDER_EXCHANGE,type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.ORDER_PAY_KEY
    ))
    // 监听订单支付成功
    public void listenPay(OrderBasicDTO order){
        //1,健壮性处理
        if (order == null || order.getUserId() == null || CollUtils.isEmpty(order.getCourseIds())){
            log.error("订单支付，异常消息，信息未空", order);
            return;
        }
        //2,添加课程学习记录
        log.debug("处理订单支付消息：{}", order);
        lessonService.addLesson(order.getUserId(),order.getCourseIds());

    }
}
